const { LabelModel } = require('../model/DataModel')

class LabelService {
  async create(data) {
    return await LabelModel.create(data)
  }

  async getLabels() {
    const list = await LabelModel.find({}).populate('categoryId', { categoryName: 1 }).exec()
    const count = await LabelModel.countDocuments()
    return { list, count }
  }

  async removeLabel(id) {
    return await LabelModel.deleteOne({ _id: id })
  }

  async updateLabel(id, data) {
    return await LabelModel.updateOne({ _id: id }, { ...data })
  }

  async getLabelById(id) {
    return await LabelModel.findById(id).populate('categoryId', { categoryName: 1 }).exec()
  }

  async getLabelByCategory(categoryId) {
    return await LabelModel.find({ categoryId })
  }
}

module.exports = new LabelService()